import React from 'react'

export default function Simpleness({ walletInfo, setTextVal }) {

    const handelClickGetEthBalance = async () => {
        const web3 = walletInfo.web3
        const balance = await web3.eth.getBalance(walletInfo.account)
        const balance_2 = web3.utils.fromWei(balance, 'ether')
        setTextVal(`ETH 的余额为：${balance_2} ETH`)
    }

    const handelClickGetBlock = async () => {
        const web3 = walletInfo.web3
        const block = await web3.eth.getBlockNumber()
        setTextVal(`当前最新的区块高度为：${block}`)
    }

    const handelClickGetGasPrice = async () => {
        const web3 = walletInfo.web3
        // 根据最近几个区块，计算平均Gas价格
        const gas = await web3.eth.getGasPrice()
        const gas_2 = web3.utils.fromWei(gas, 'ether')
        setTextVal(`最近区块平均Gas价格：${gas_2} ETH`)
    }

    return (
        walletInfo && <div>
            <h3>简单操作：</h3>
            <button onClick={handelClickGetEthBalance}>获取ETH的余额</button>
            <span>  </span>
            <button onClick={handelClickGetBlock}>查询区块高度</button>
            <span>  </span>
            <button onClick={handelClickGetGasPrice}>查询平均Gas费</button>
        </div>
    )
}
